package com.jiaquyun.jcyx.main.adapter

import android.os.Bundle
import android.view.ViewGroup
import android.widget.ImageView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.ConvertUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.jiaquyun.jcyx.R
import com.jiaquyun.jcyx.entity.Banner
import com.jiaquyun.jcyx.entity.Cat
import com.jiaquyun.jcyx.entity.GoodsGroup
import com.jiaquyun.jcyx.entity.SeckillList
import com.jiaquyun.jcyx.main.goods.GoodsDetailsActivity
import com.module.lemlin.base.RecyclerViewHolder
import com.jiaquyun.jcyx.utils.SpanUtils
import com.module.lemlin.image.loadCropImage
import com.module.lemlin.image.loadRoundImage
import com.youth.banner.adapter.BannerAdapter
import java.lang.StringBuilder

class HomeBannerAdapter :
    BannerAdapter<Banner, RecyclerViewHolder>(mutableListOf()) {
    override fun onCreateHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        val imageView = ImageView(parent.context)
        imageView.layoutParams = ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        imageView.setPadding(
            ConvertUtils.dp2px(13f), 0,
            ConvertUtils.dp2px(13f), 0
        )
        imageView.scaleType = ImageView.ScaleType.CENTER_CROP
        return RecyclerViewHolder(imageView)
    }

    override fun onBindView(holder: RecyclerViewHolder, data: Banner, position: Int, size: Int) {
        (holder.itemView as ImageView).loadRoundImage(data.image, radius = 15)
    }
}

class HomeCategoryAdapter :
    BaseQuickAdapter<Cat, BaseViewHolder>(R.layout.item_home_cate) {

    override fun convert(holder: BaseViewHolder, item: Cat) {
        holder.setText(R.id.tvCategoryName, item.name)
            .getView<ImageView>(R.id.ivCategoryIcon)
            .loadCropImage(item.image)
    }

}


class HomeOnePriceAdapter :
    BaseQuickAdapter<GoodsGroup, BaseViewHolder>(R.layout.item_home_one_price) {
    init {
        setOnItemClickListener { _, _, position ->
            val itemData = data[position]
            val bundle = Bundle().apply {
                putInt(GoodsDetailsActivity.KEY_GOODS_ID, itemData.id)
            }
            ActivityUtils.startActivity(bundle, GoodsDetailsActivity::class.java)
        }
    }
    override fun convert(holder: BaseViewHolder, item: GoodsGroup) {
        holder.setText(
            R.id.tvGoodsPrice,
            SpanUtils.setMoneySymbol(item.guidance_price, item.price, 0.6f, 0.55f)
        )
            .getView<ImageView>(R.id.ivGoodsIcon)
            .loadCropImage(item.image)
    }
}


class HomeSeckillAdapter :
    BaseQuickAdapter<SeckillList, BaseViewHolder>(R.layout.item_home_seckill) {
    init {
        setOnItemClickListener { _, _, position ->
            val itemData = data[position]
            val bundle = Bundle().apply {
                putInt(GoodsDetailsActivity.KEY_GOODS_ID, itemData.id)
            }
            ActivityUtils.startActivity(bundle, GoodsDetailsActivity::class.java)
        }
    }
    override fun convert(holder: BaseViewHolder, item: SeckillList) {
        holder.setText(R.id.tvSeckillName, item.goods_name)
            .setText(R.id.tvSeckillPrice, StringBuilder("￥").append(item.price))
            .getView<ImageView>(R.id.ivSeckillIcon)
            .loadCropImage(item.image)
    }
}

